package com.yq.testmodule.second_floor;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.yq.testmodule.R;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.ArrayList;
import java.util.List;

import config.RouterAddressKt;

@Route(path = RouterAddressKt.TEST_SecondFloor)
public class SecondFloorActivity extends AppCompatActivity {


    public MySecondFloorView secondFloorView;
    ConstraintLayout clFloor;
    DemoCollectionPagerAdapter demoCollectionPagerAdapter;

    Banner banner;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_second_floor);
        demoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.pager);
        TabLayout tabLayout = findViewById(R.id.tab_layout);
        banner = findViewById(R.id.banner);
        if (viewPager == null) {
            return;
        } else {
            viewPager.setAdapter(demoCollectionPagerAdapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        }

        secondFloorView = findViewById(R.id.cl_second);
        clFloor = findViewById(R.id.cl_floor);
        TextView tvSearch = findViewById(R.id.tv_search);
        viewPager.post(new Runnable() {
            @Override
            public void run() {
                int height = tabLayout.getHeight() + tvSearch.getHeight();
                secondFloorView.setInitPosition(height);
            }
        });
        secondFloorView.addScrollerListener(dy -> {
            //滚动距离 在指定范围之内就从透明到显示
            float dis = UIUtil.dip2px(getBaseContext(), 30);
            float alpha = dy / dis;
            if (alpha > 1) {
                alpha = 1;
            }
            if (alpha == 0) {
                clFloor.setVisibility(View.GONE);
            } else {
                if (clFloor.getVisibility() == View.GONE) {
                    clFloor.setVisibility(View.VISIBLE);
                }
            }
            clFloor.setAlpha(alpha);
        });

        List<String> images = new ArrayList<String>() {{
            add("https://th.bing.com/th?id=OSK.TMLNls2UL93bMP2fA-QhLG5dIKCRZkrcUg-Q4Ajl8DVpeeU&w=150&h=220&c=7&o=6&pid=SANGAM");
            add("https://th.bing.com/th?id=OSK.TMLNBdGYmyz21RaR69ACd-zMgaQdwXyn4MDVIMzcC40QL9k&w=150&h=220&c=7&o=6&pid=SANGAM");
            add("https://th.bing.com/th?id=OSK.TMLNFHGgG7c5FlHHkOunJkLOmsYbBac4HnrZt3_GxsrX_uE&w=150&h=220&c=7&o=6&pid=SANGAM");
            add("https://th.bing.com/th?id=OSK.TMLNUJQ6OgG0WuVWtr-HkmxMK2kM6PAgpb7hZ7ca4_g9LWI&w=150&h=220&c=7&o=6&pid=SANGAM");
        }};
        List<DataBean> list = new ArrayList<>();
        for (String image : images) {
            list.add(new DataBean(image));
        }

        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new ImageAdapter(list))

//                .setPageTransformer(new ZoomOutPageTransformer())
               ;
        //添加魅族效果
        banner.setBannerGalleryEffect(20,10);

    }


    class DataBean {
        public String img;

        public DataBean(String img) {
            this.img = img;
        }
    }

    /**
     * 自定义布局，下面是常见的图片样式，更多实现可以看demo，可以自己随意发挥
     */
    public class ImageAdapter extends BannerAdapter<DataBean, ImageAdapter.BannerViewHolder> {

        public ImageAdapter(List<DataBean> mDatas) {
            //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
            super(mDatas);
        }

        //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
        @Override
        public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return new BannerViewHolder(imageView);
        }

        @Override
        public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) {
            Glide.with(holder.imageView).load(data.img).into(holder.imageView);
            holder.imageView.setOnClickListener(v -> Toast.makeText(SecondFloorActivity.this, "图片点击了", Toast.LENGTH_SHORT).show());
        }

        class BannerViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public BannerViewHolder(@NonNull ImageView view) {
                super(view);
                this.imageView = view;
            }
        }
    }


    public static class DemoCollectionPagerAdapter extends FragmentPagerAdapter {

        public DemoCollectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if(i == 2){
                return new TestFragment2();
            }
            return new TestFragment();
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}
